#include "ntc_cal.h"

const uint16_t NTCTAB[136] =
    {
        0xEAD, 0xE9D, 0xE8D, 0xE7B, 0xE69, 0xE57, 0xE43, 0xE30, 0xE1B, 0xE06, 0xDF0, 0xDDA, 0xDC3, 0xDAB, 0xD93, 0xD79, 0xD60, 0xD45, 0xD2A, 0xD0F, 0xCF2, 0xCD5, 0xCB7, 0xC99, 0xC7A, 0xC5B, 0xC3B, 0xC1A, 0xBF9, 0xBD7, 0xBB5, 0xB92, 0xB6F, 0xB4B, 0xB27, 0xB02, 0xADD, 0xAB8, 0xA92, 0xA6D, 0xA46, 0xA20, 0x9F9, 0x9D3, 0x9AC, 0x985, 0x95E, 0x937, 0x910, 0x8E8, 0x8C1, 0x89A, 0x873, 0x84D, 0x826, 0x800, 0x7D9, 0x7B3, 0x78D, 0x768, 0x743, 0x71E, 0x6F9, 0x6D5, 0x6B2, 0x68E, 0x66B, 0x649, 0x627, 0x605, 0x5E4, 0x5C3, 0x5A3, 0x583, 0x564, 0x545, 0x527, 0x509, 0x4EC, 0x4CF, 0x4B3, 0x498, 0x47D, 0x462, 0x448, 0x42E, 0x415, 0x3FD, 0x3E4, 0x3CD, 0x3B6, 0x39F, 0x389, 0x374, 0x35F, 0x34A, 0x336, 0x323, 0x310, 0x2FD, 0x2EA, 0x2D9, 0x2C7, 0x2B6, 0x2A5, 0x295, 0x285, 0x276, 0x267, 0x258, 0x24A, 0x23C, 0x22E, 0x221, 0x214, 0x207, 
		0x1FB, 0x1EE, 0x1E3, 0x1D7, 0x1CC, 0x1C1, 0x1B7, 0x1AC, 0x1A2, 0x198, 0x18F, 0x186, 0x17D, 0x174, 0x16B, 0x163, 0x15B, 0x153, 0x14B, 0x144};
#define TEMP_CAIL_VALUE 0 
/*

*/
uint8_t look_up_table(const uint16_t *a, uint8_t ArrayLong, uint16_t data)
{
    uint8_t i;
    uint16_t min_diff = 0xFFFF;
    uint8_t closest_index = 0;

    // 线性查找最接近的值
    for(i = 0; i < ArrayLong; i++)
    {
        uint16_t diff = (data > a[i]) ? (data - a[i]) : (a[i] - data);
        if(diff < min_diff)
        {
            min_diff = diff;
            closest_index = i;
        }
    }

    return closest_index;
}
float num_to_temperature(uint8_t num)
{
    float data;
    data = num - 30;
    return data;
}

uint16_t get_ntc_Temperature(uint16_t data)
{
    int num, t1;
    num = look_up_table(NTCTAB, 136, data);
    t1 = num_to_temperature(num);
    return t1 - TEMP_CAIL_VALUE;
}
